package com.beyondsoft.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.util.Log;

public class BookProvider extends ContentProvider {

	private static final int BOOKS = 0;
	private static final int BOOK_ID = 1;
	private static final String AUTHORITY = "com.beyondsoft";
	private static UriMatcher MATCHER;

	static {
		MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
		MATCHER.addURI(AUTHORITY, "books", BOOKS);
		MATCHER.addURI(AUTHORITY, "books/#", BOOK_ID);
	}

	private SQLiteOpenHelper openHelper;

	@Override
	public boolean onCreate() {
		openHelper = new DatabaseHelper(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String table) {

		SQLiteDatabase db = openHelper.getReadableDatabase();
		SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
		builder.setTables(DatabaseHelper.table);
		int match = MATCHER.match(uri);
		Cursor cursor = null;

		if (match == BOOK_ID) {
			builder.appendWhere("id=" + uri.getPathSegments().get(1));
		} else if (match == BOOKS) {
		} else {
			Log.i("sys", "this is uri is error when I query it!");
		}

		// db, projectionIn, selection, selectionArgs, groupBy, having,
		// sortOrder
		cursor = builder.query(db, projection, selection, selectionArgs, null,
				null, null);
		cursor.setNotificationUri(getContext().getContentResolver(), uri);
		// db.close(); 注：在此先不要关，否则取不到数据

		return cursor;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {

		SQLiteDatabase db = openHelper.getWritableDatabase();

		long id = db.insert(DatabaseHelper.table, null, values);
		getContext().getContentResolver().notifyChange(uri, null);
		db.close();

		return ContentUris.withAppendedId(uri, id);
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {

		SQLiteDatabase db = openHelper.getWritableDatabase();

		int id = db.delete(DatabaseHelper.table, selection, selectionArgs);
		if (id < 0) {
			Log.i("sys", "this is uri is error when I delete it");
		}
		getContext().getContentResolver().notifyChange(uri, null);
		db.close();

		return id;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {

		SQLiteDatabase db = openHelper.getWritableDatabase();

		int id = db.update(DatabaseHelper.table, values, selection,
				selectionArgs);
		if (id < 0) {
			Log.i("sys", "this is uri is error when I update it");
		}
		getContext().getContentResolver().notifyChange(uri, null);
		db.close();

		return id;
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}
}
